﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ComponentModel.Composition;

using NuMetaheuristics.Genotypes;
using NuMetaheuristics.Operators.Vector;
using NuMetaheuristics.Operators.Value;

namespace NuMetaheuristics.Contexts.Vector.Double
{
    [DataContract(Namespace = "http://numetaheuristics.codeplex.com/schemas")]
    [Export(typeof(IContext<DoubleVectorGenotype>))]
    public class Rosenbrock : DoubleVectorContext
    {
        public Rosenbrock()
            : base()
        {
        }

        public Rosenbrock(int length)
            : base(length, -2.048, 2.048)
        {
        }

        override public void Evaluate(ref DoubleVectorGenotype genome)
        {
            double total = 6 * genome.Vector.Length;

            for(uint i = 0; i < (genome.Vector.Length - 1); i++)
            {
                double x = genome.Vector[i];
                double a = genome.Vector[i+1] - (x * x);
                double b = (100 * (a) * (a));
                double c = ((1 - x) * (1 - x));

                total += b + c;
            }

            genome.Fitness = new FitnessSmallerIsBetter(total);
        }
    }
}
